package com.sdap2.controller;

import com.sdap2.common.model.ApiResult;
import com.sdap2.dto.GreenhouseDTO;
import com.sdap2.entity.Greenhouse;
import com.sdap2.service.GreenhouseService;
import com.sdap2.vo.GreenhouseVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

@Tag(name = "大棚管理")
@RestController
@RequestMapping("/api/greenhouses")
@RequiredArgsConstructor
public class GreenhouseController {
    
    private final GreenhouseService greenhouseService;
    
    @Operation(summary = "创建大棚")
    @PostMapping
    public ApiResult<GreenhouseVO> create(
            @Parameter(description = "大棚信息") @Valid @RequestBody GreenhouseDTO dto,
            @Parameter(description = "认证token") @RequestHeader("Authorization") String token) {
        Greenhouse greenhouse = greenhouseService.create(dto, token);
        return ApiResult.success(GreenhouseVO.fromEntity(greenhouse));
    }
    
    @Operation(summary = "更新大棚")
    @PutMapping("/{id}")
    public ApiResult<GreenhouseVO> update(
            @Parameter(description = "大棚ID") @PathVariable Long id,
            @Parameter(description = "大棚信息") @Valid @RequestBody GreenhouseDTO dto,
            @Parameter(description = "认证token") @RequestHeader("Authorization") String token) {
        Greenhouse greenhouse = greenhouseService.update(id, dto, token);
        return ApiResult.success(GreenhouseVO.fromEntity(greenhouse));
    }
    
    @Operation(summary = "删除大棚")
    @DeleteMapping("/{id}")
    public ApiResult<Void> delete(
            @Parameter(description = "大棚ID") @PathVariable Long id,
            @Parameter(description = "认证token") @RequestHeader("Authorization") String token) {
        greenhouseService.delete(id, token);
        return ApiResult.success();
    }
    
    @Operation(summary = "获取大棚详情")
    @GetMapping("/{id}")
    public ApiResult<GreenhouseVO> getById(
            @Parameter(description = "大棚ID") @PathVariable Long id,
            @Parameter(description = "认证token") @RequestHeader("Authorization") String token) {
        Greenhouse greenhouse = greenhouseService.getById(id, token);
        return ApiResult.success(GreenhouseVO.fromEntity(greenhouse));
    }
    
    @Operation(summary = "分页查询大棚")
    @GetMapping
    public ApiResult<Page<GreenhouseVO>> getPage(
            @Parameter(description = "分页参数") @PageableDefault(size = 10) Pageable pageable,
            @Parameter(description = "认证token") @RequestHeader("Authorization") String token) {
        Page<Greenhouse> page = greenhouseService.getPage(pageable, token);
        return ApiResult.success(page.map(GreenhouseVO::fromEntity));
    }
    
    @Operation(summary = "更新大棚访问令牌")
    @PutMapping("/{id}/access-token")
    public ApiResult<GreenhouseVO> updateAccessToken(
            @Parameter(description = "大棚ID") @PathVariable Long id,
            @Parameter(description = "访问令牌") @RequestParam String accessToken,
            @Parameter(description = "认证token") @RequestHeader("Authorization") String token) {
        Greenhouse greenhouse = greenhouseService.updateAccessToken(id, accessToken, token);
        return ApiResult.success(GreenhouseVO.fromEntity(greenhouse));
    }
    
    @Operation(summary = "根据boxNo获取大棚")
    @GetMapping("/box/{boxNo}")
    public ApiResult<GreenhouseVO> getByBoxNo(
            @Parameter(description = "大棚编号") @PathVariable String boxNo,
            @Parameter(description = "认证token") @RequestHeader("Authorization") String token) {
        Greenhouse greenhouse = greenhouseService.getByBoxNo(boxNo, token);
        return ApiResult.success(GreenhouseVO.fromEntity(greenhouse));
    }
} 